package cn.com.self.apis.portal.controller;

import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.portal.contants.StatusCode;
import cn.com.self.apis.portal.entity.DatatypeEntity;
import cn.com.self.apis.portal.entity.DatatypeGroupEntity;
import cn.com.self.apis.portal.service.intf.IDatatypeService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.portal.util.StringUtils;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.Constants;
import cn.com.selfsoft.smartcity.commons.exception.OutOfBusinessException;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import cn.com.selfsoft.smartcity.core.controller.GenericControllerImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 数据类型 前端控制器
 */
@RestController
@Api(tags = "字典数据信息类")
public class DatatypeController extends GenericControllerImpl<DatatypeEntity, String> {

    @Autowired
    private IDatatypeService datatypeService;

    /**
     * 获取字典数据数目
     *
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatype"}, method = RequestMethod.GET, params = {Constants.DEFAULT_MARK_PARAMETER + "=count"})
    @ApiOperation(value = "获取字典数据数目")
    public ResponseEntity count(HttpServletRequest request) {
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark","access_token");
        Long total = this.datatypeService.count(queryMap);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(total));
    }

    /**
     * 分页获取字典数据
     *
     * @param request
     * @param page
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatype"}, method = RequestMethod.GET, params = {Constants.DEFAULT_MARK_PARAMETER + "=page"})
    @ApiOperation(value = "分页获取字典数据")
    public ResponseEntity page(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","timestamp","access_token");
        ICustomPage<DatatypeEntity> pageData = this.datatypeService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }

    /**
     * 获取字典数据
     *
     * @param request
     * @param sort
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatype"}, method = RequestMethod.GET, params = {Constants.DEFAULT_MARK_PARAMETER + "=list"})
    @ApiOperation(value = "获取字典数据")
    public ResponseEntity list(HttpServletRequest request, Sort sort) {
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark","timestamp","access_token");
        List<DatatypeEntity> list = this.datatypeService.list(queryMap, sort);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(list));
    }

    /**
     * 获取字典数据
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatype/load/{id}"}, method = RequestMethod.GET)
    @ApiOperation(value = "获取字典数据")
    public ResponseEntity load(@PathVariable("id") String id, HttpServletRequest request) {
        DatatypeEntity datatypeEntity = this.datatypeService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(datatypeEntity));
    }
    /**
     * 通过字典类型获取字典数据集合
     *
     * @param code
     * @param request
     * @return
     */

    @RequestMapping(value = {"/datatype/code/{code}"}, method = RequestMethod.GET)
    @ApiOperation(value = "通过字典类型获取字典数据集合")
    public ResponseEntity getCode(@PathVariable("code") String code, HttpServletRequest request) {
        List<DatatypeEntity> datatypeEntity = datatypeService.zdList(code);
        List<DatatypeEntity> datatypeEntitys = new LinkedList<>();

            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(datatypeEntitys));
    }
    /**
     * 删除字典数据
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatype/delete/{id}"}, method = RequestMethod.DELETE)
    @ApiOperation(value = "删除字典数据")
    public ResponseEntity delete(@PathVariable("id") String id, HttpServletRequest request) {
        DatatypeEntity oldentity=datatypeService.load(id);
        this.datatypeService.delete(id);
        //删除redis缓存
        datatypeService.updateRedisDatatype(oldentity, false);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    /**
     * 创建字典数据
     *
     * @param entity
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatype/create"}, method = RequestMethod.POST)
    @ApiOperation(value = "创建字典数据")
    public ResponseEntity create(@RequestBody DatatypeEntity entity, HttpServletRequest request) {

        if(entity.getGroupId() ==null ){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(null));
        }
        DatatypeEntity datatypeEntity = this.datatypeService.create(entity);
        //更新redis缓存
        datatypeService.updateRedisDatatype(datatypeEntity, true);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(datatypeEntity));
    }

    /**
     * 修改字典数据
     *
     * @param id
     * @param entity
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatype/update/{id}"}, method = RequestMethod.POST)
    @ApiOperation(value = "修改字典数据")
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody DatatypeEntity entity, HttpServletRequest request) {
        DatatypeEntity datatypeEntity = this.datatypeService.load(id);
        if (datatypeEntity != null) {
            CopyUtils.copyProperties(entity, datatypeEntity);
            this.datatypeService.update(datatypeEntity);
            //更新redis缓存
            datatypeService.updateRedisDatatype(datatypeEntity, true);
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(datatypeEntity));
        } else {
            throw new OutOfBusinessException("id不存在");
        }
    }

    /**
     * 根据字典类型或类型id获取字典详情
     *
     * @param
     * @param request
     * @return
     */
    @RequestMapping(value = {"/datatype/bygrouptypeOrID"}, method = RequestMethod.GET)
    @ApiOperation(value = "根据字典类型或类型id获取字典详情")
    public ResponseEntity Datatypes(Long groupId,String grouptype, HttpServletRequest request, Sort sort) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(),"access_token");
        List<DatatypeEntity> list=null;
        if(groupId !=null  || StringUtils.isNoneBlank(grouptype)){
            list=this.datatypeService.getListBygroupIdOrgrouptype(groupId,grouptype);
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(list));

    }
}
